// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UIControl.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.UI.Forms
{
    #region Using Directives

    using System;
    using System.ComponentModel;
    using System.Windows.Forms;

    using Interfaces.UI;

    using Org.OpenTrader.Framework.Enums;
    using Org.OpenTrader.Framework.Events;

    #endregion

    /// <summary>
    /// The ui control.
    /// </summary>
    public partial class UIControl : UserControl, IUIControl
    {
        #region Constants and Fields

        /// <summary>
        /// The containingForm.
        /// </summary>
        private IUIForm containingForm;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="UIControl"/> class.
        /// </summary>
        public UIControl()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UIControl"/> class.
        /// </summary>
        /// <param name="container">
        /// The container.
        /// </param>
        public UIControl(IContainer container)
        {
            container.Add(this);
            this.InitializeComponent();
        }

        #endregion

        #region Events

        /// <summary>
        /// The containing form created event cb.
        /// </summary>
        public static event EventHandler<FormEventArgs> ContainingFormEventCB;

        #endregion

        #region Properties

        /// <summary>
        /// Gets ContainingForm.
        /// </summary>
        [Browsable(false)]
        public IUIForm ContainingForm
        {
            get
            {
                return this.containingForm;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// The create containing form.
        /// </summary>
        /// <param name="title">
        /// The title.
        /// </param>
        /// <param name="layoutType">
        /// The layoutType.
        /// </param>
        /// <param name="closeType">
        /// The closeType.
        /// </param>
        /// <returns>
        /// </returns>
        public IUIForm CreateContainingForm(string title, EUIFormLayoutType layoutType, EUIFormClosingType closeType)
        {
            // Devel.ExecutedFrom("UIControl.CreateContainingForm");
            this.containingForm = new UIForm(title, layoutType, closeType, this);

            RaiseContainingFormEvent(this, new FormEventArgs.Created());

            return this.containingForm;
        }

        /// <summary>
        /// The create containing form.
        /// </summary>
        /// <param name="title">
        /// The title.
        /// </param>
        /// <returns>
        /// </returns>
        public IUIForm CreateContainingForm(string title)
        {
            this.containingForm = this.CreateContainingForm(title, EUIFormLayoutType.TitleBarAtTop, EUIFormClosingType.Hard);
            return this.containingForm;
        }

        #endregion

        #region Methods

        /// <summary>
        /// The on handle created.
        /// </summary>
        /// <param name="e">
        /// The e.
        /// </param>
        protected override void OnHandleCreated(EventArgs e)
        {
            // Devel.ExecutedFrom("UIForm.OnHandleCreated " + GetHashCode());
            base.OnHandleCreated(e);
        }

        /// <summary>
        /// The on handle destroyed.
        /// </summary>
        /// <param name="e">
        /// The e.
        /// </param>
        protected override void OnHandleDestroyed(EventArgs e)
        {
            // Devel.ExecutedFrom("UIForm.OnHandleDestroyed " + GetHashCode());
            base.OnHandleDestroyed(e);
        }

        /// <summary>
        /// The fire containing form created event.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="args">
        /// The args.
        /// </param>
        private static void RaiseContainingFormEvent(object sender, FormEventArgs args)
        {
            if (ContainingFormEventCB != null)
            {
                ContainingFormEventCB(sender, args);
            }
        }

        #endregion
    }
}